import VueRouter from "vue-router";
import HomeView from "@/views/HomeView.vue";
import GoodsShow from "@/components/GoodsShow.vue";
import GoodsDetails from "@/components/GoodsDetails.vue";
import UserOrder from "@/components/UserOrder.vue";
import ShoppingCart from "@/components/ShoppingCart.vue";
import UserLogin from "@/components/UserLogin.vue";
import LoginCom from "@/components/LoginCom.vue";
import Ptype from "@/components/Ptype.vue";
import Shangpin from "@/components/Shangpin.vue";


const router = new VueRouter({
    routes: [
        {
            // 登陆页面
            path: '/loginCom',
            component: LoginCom,

        },
        {
            //主界面组件
            path: '/',
            component: HomeView,
            //声明子路由
            children: [
                {
                    //商品展示组件
                    path: '',
                    component: GoodsShow,
                }, {
                    //商品详情组件
                    path: 'goodsDetails',
                    component: GoodsDetails,
                }, {
                    //用户订单组件
                    path: 'userOrder',
                    component: UserOrder,
                }, {
                    // 购物车组件
                    path: 'shoppingCart',
                    component: ShoppingCart,
                }, {
                    //用户页面
                    path: 'userLogin',
                    component: UserLogin,
                },
                {
                    // 王菲增删改查
                    path: 'ptype',
                    component: Ptype,
                },
                {
                    // 梁婧娴增删改查
                    path: 'shangpin',
                    component: Shangpin,
                }
            ]
        }

    ]
})

// 路由的前置守卫
router.beforeEach((to, from, next) => {
    let user = sessionStorage.getItem("user_info");
    console.info(user)
    // 如果未登录，并且要跳转shoppingCart userOrder两个组件
    if (!user && (to.path === "/shoppingCart" || to.path === "/userOrder")) {
        //跳转到登录界面
        next("/loginCom");
    } else {
        //继续往下执行
        next()
    }


})

export default router
